<?php
include_once(DIR_LIBRARY . "/request.php");
class Controller
{
    public $request;
    private $debug;

    public function __construct()
    {
        $this->request = new Request();
        $this->debug = $_SESSION['debug'];
    }

    public function invoke()
    {
        if ($this->request->server['REQUEST_METHOD'] == 'POST' || $this->request->server['REQUEST_METHOD'] == 'GET') {
            if (isset($this->request->get['facebook_default'])) {
                include_once(DIR_MODEL . "/facebook_api.php");
                $model_facebook_api = new facebookApi(true);
                $data_get = $model_facebook_api->getInformationGroup();
                $model_facebook_api->pushDataIntoDB($data_get);
                while (isset($data_get['paging']['next'])) {
                    $url = parse_url($data_get['paging']['next']);
                    $query = array();
                    parse_str($url['query'], $query);
                    $until = '&until=' . $query['until'];
                    $since = '&since=' . $query['since'];
                    $model_facebook_api->updatePaging($until,$since);
                    $data_get = $model_facebook_api->getInformationGroup($until);
                    $model_facebook_api->pushDataIntoDB($data_get);
                }
//                $this->debug->w($data_get,1);
                // after load data and push into db , redirect to facebook group .
                //$this->redirect(HTTP_SERVER . 'index.php?facebook_group=1');
            } else if (isset($this->request->get['facebook_group'])) {
                include_once(DIR_MODEL . "/facebook_api.php");
                $model_facebook_api = new facebookApi();
                if (isset($this->request->get['page'])) {
                    $page = $this->request->get['page'];
                } else {
                    $page = 1;
                }
                $limit = (int)DEFAULT_LIMIT;
                $data = array('start' => ($page - 1) * $limit,
                              'limit' => $limit);
                $url = '';
                if(isset($this->request->get['sort']) && !empty($this->request->get['sort'])){
                    $data['sort'] = $this->request->get['sort'];
                    $sort = $this->request->get['sort'];
                }else{
                    $sort = '';
                }

                if(isset($this->request->get['order']) && !empty($this->request->get['order'])){
                    $data['order'] = $this->request->get['order'];
                    $order = $this->request->get['order'];
                }else{
                    $order = 'ASC';
                }

                if(isset($this->request->get['hashtab']) && !empty($this->request->get['hashtab'])){
                    $data['hashtab'] = '#'.$this->request->get['hashtab'];
                    $url .= '&hashtab='.$this->request->get['hashtab'];
                }

                $data_render = $model_facebook_api->getInformationGroupByDatabase($data);
                $text_sort_comment = 'Increase By Comments';
                $text_sort_like = 'Increase By Likes';
                $base_link = HTTP_SERVER.'index.php?facebook_group=1';
                if(!empty($sort)){
                    if($sort == 'post_comments' && $order == 'ASC'){
                        $text_sort_comment = 'Decrease By Comments';
                        $next_order_comment = 'DESC';
                    }elseif($sort == 'post_comments' && $order == 'DESC'){
                        $text_sort_comment = 'Increase By Comments';
                        $next_order_comment = 'ASC';
                    }else{
                        $next_order_comment = $order;
                    }
                    if($sort == 'post_likes' && $order == 'ASC'){
                        $text_sort_like = 'Decrease By Likes';
                        $next_order_like = 'DESC';
                    }elseif($sort == 'post_likes' && $order == 'DESC'){
                        $text_sort_like = 'Increase By Likes';
                        $next_order_like = 'ASC';
                    }else{
                        $next_order_like = $order;
                    }
                }else{
                    $next_order_comment = $order;
                    $next_order_like = $order;
                }

                $sort_comment = HTTP_SERVER.'index.php?facebook_group=1&sort=post_comments&order='.$next_order_comment.$url;
                $sort_like = HTTP_SERVER.'index.php?facebook_group=1&sort=post_likes&order='.$next_order_like.$url;

                include DIR_VIEW . '/facebook_layout.php';
            } else {
                echo 'nothing.........................';
            }
        }
    }


    public function redirect($url, $status = 302)
    {
        header('Status: ' . $status);
        header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
        exit();
    }
}

?>